/**
 *@NApiVersion 2.x
 *@NScriptType UserEventScript
 */
define(['N/record', 'N/render', 'N/file'], function (record, render, file) {
    function beforeLoad(context) {
        if (context.type === 'view') {
            try {
                var employee = record.load({
                    type: record.Type.EMPLOYEE,
                    id: context.newRecord.id,
                    isDynamic: true
                });
                var label = {};
                label.employeeId = employee.getValue('entityid');
                label.employeeName = employee.getValue('altname');
                //调用模板文件
                var renderer = render.create();
                renderer.templateContent = file.load(529).getContents();
                renderer.addCustomDataSource({
                    format: render.DataSource.JSON,
                    alias: 'label',
                    data: JSON.stringify(label)
                });
                var pdfFile = renderer.renderAsPdf();
                //PDF文件存放文件夹ID
                pdfFile.folder = 185;
                pdfFile.name = label.employeeId + '.pdf';
                var fileId = pdfFile.save();
                var File = file.load(fileId);
                var pdfurl = File.url;
                var form = context.form;
                form.addButton({
                    id: 'custpage_button',
                    label: '打印员工列表标签',
                    functionName: "window.open('" + pdfurl + "');"
                });
            } catch (e) {
                log.error(e.name, e.message);
            }

        }
    }


    return {
        beforeLoad: beforeLoad
    };
});
